from flask import Blueprint, jsonify, request

from app.forms.book import SearchForm
from app.libs.helper import is_isbn_or_key
from app.spider.yushu_book import YuShuBook

web=Blueprint('web',__name__)
@web.route('/book/search')
def seach():

    q=request.args['q']
    page=request.args['page']
    form=SearchForm(request.args)
    if form.validate():
        q = form.q.data.strip()
        page = form.page.data
        isbn_or_key = is_isbn_or_key(q)
        if isbn_or_key=='isbn':
            result=YuShuBook.serch_by_isbn(q)
        else:
            result = YuShuBook.serch_by_keyword(q)
        return jsonify(result)
    else:
        return jsonify({'msg':"参数校验失败",'code':200})

@web.route("/hello")
def hello():
    return "hello"